/*
 * Copyright (C) 2008 xyzse
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

/**
 * @file XDocIndex.cc
 * @version 0.0.0
 * @author zouxin ( zouxin2008@gmail.com )
 * @date 18/3/2008 0.0.0 created, by zouxin
 */
#include "XDocIndex.h"
#include "CMd5.h"
#include <string>
#include <iostream>
#include <fstream>


/**
 * inputfile:  rawData.raw
 * outputfile: doc.index
 */
XDocIndex::XDocIndex(string inputfile,string outputfile)
{
	m_input.open(inputfile.c_str(),ios::binary);
	if(!m_input)
	{
		cerr<<"can not open "<<inputfile<<endl;
		exit(-1);
	}

	m_output.open(outputfile.c_str(),ios::binary|ios::out);
	if(!m_output)
	{
		cerr<<"can not open "<<outputfile<<endl;
		exit(-1);
	}
}

XDocIndex::~XDocIndex()
{
	m_input.close();
	m_output.close();
}

//if return 0,success
int XDocIndex::beginIndex()
{
	
	string temp,url,ip;
	bool flag=false,flagbegin=false;
	fstream::pos_type offset;
	int docid=0;
	CMD5 urldigest;
	while(getline(m_input,temp))
	{
		if(temp=="version: 1.0")
		{
			offset=m_input.tellg();
			offset-=13;//minus the length of "version: 1.0\n"
			flag=true;//found the start 
			continue;
		}
		if(flag)
		{
			if(temp.length()>=5&&temp.substr(0,5)=="url: ")
			{
				url=temp.substr(5);
				flagbegin=true;
			}else if(temp.length()>=4&&temp.substr(0,4)=="ip: ")
			{
				ip=temp.substr(4);
				flagbegin=false;
			}else
				break;//the header is not right,so goto next
			if(flagbegin==false)
			{
				urldigest.GenerateMD5((unsigned char *)url.c_str(),url.length());
				m_output<<docid++<<"\t";
				m_output<<offset<<"\t"<<url<<"\t";
				m_output<<urldigest.ToString()<<"\n";
				//cout<<url<<endl;
				//cout<<ip<<endl;
			}
		}
		if(flagbegin==false)
		{
			flag=false;
			//url="";
			//ip="";
		}
	}
	return 0;
}

